<?php
class ArtAction extends BaseAction{
	
	public function show(){
		//查询文章内容
		$art = D('Art');
		//组合分页信息
		$limit = C('url_num_admin');
		$where = '1';
		if($_GET['typeid']) $where .= " and typeid=".$_GET['typeid'];
		if(isset($_GET['status']) && $_GET['status']!=2) $where .= " and status=".$_GET['status'];
		if($_POST['wd'])$where .= " and title like '%$_POST[wd]%'";
		$count = $art->where($where)->count('id');
		$totalpages = ceil($count/$limit);
		$currentpage = !empty($_GET['p'])?intval($_GET['p']):1;
		$currentpage = get_maxpage($currentpage,$totalpages);
		$pageurl = U('Admin-Art/Show-typeid-'.$_GET['typeid'].'-status-'.$_GET['status'],$admin,false,false).'-p-{!page!}'.C('url_html_suffix');
		$admin['p'] = $currentpage;$_SESSION['news_jumpurl'] = U('Admin-Art/Show',$admin).C('url_html_suffix');
		$pages = '共'.$count.'篇文章&nbsp;当前:'.$currentpage.'/'.$totalpages.'页&nbsp;'.getpage($currentpage,$totalpages,8,$pageurl,'pagego(\''.$pageurl.'\','.$totalpages.')');
		$admin['pages'] = $pages;
		$artlist = $art->where($where)->order('addtime desc')->limit($limit)->page($currentpage)->select();
		//获取文章类别列表
		$typearr = D('art_type')->where("state='1'")->select();
		$this->assign('typearr',$typearr);
		$this->assign('artlist',$artlist);
		$this->assign('typeid',$_GET['typeid']);
		$this->assign($admin);
		$this->display('./Public/system/art_show.html');
	}
	
	public function add(){
		$artid = $_GET['artid'];
		//获取文章类别列表
		$typearr = D('art_type')->where("state='1'")->select();
		if($artid){
			$array['news_tplname'] = '编辑';
			$art = D('art')->where("id=$artid")->find();
		}else{
			$array['news_tplname'] = '添加';
		}
		$this->assign($array);
		$this->assign('art',$art);
		$this->assign('artid',$artid);
		$this->assign('typearr',$typearr);
		$this->display('./Public/system/art_add.html');
	}
	
	public function insert(){
		$ar = D('art');
		$artid = $_POST['artid'];
		$artarr = $_POST['art'];
		if(empty($artarr['title']))$this->error('标题不能为空');
		if(empty($artarr['content']))$this->error('内容不能为空');
		$artarr['uptime'] = time();
		if($artid){ //编辑
			$id = $ar->where("id='$artid'")->save($artarr);
			$message = '修改失败';
			if($id)$message = '修改成功';
		}else{ //添加
			$artarr['addtime'] = time();
			$artarr['adminname'] = '002';
			$id = $ar->add($artarr);
			$message = '添加失败';
			if($id)$message = '添加成功';
		}
		$this->success($message);
	}
	
	public function showArt(){
		$sql = "update `2k_art` set status=$_GET[status] where id='$_GET[artid]'";
		$rs = D('art');
		$rs->execute($sql);
		$this->success('修改成功');
	}
	
	public function del(){
		$artid = $_GET['artid'];
		$art = D('Art');
		$id = $art->where("id=$artid")->delete();
		$message = '删除失败';
		if($id)$message = '删除成功';
		$this->success($message);		
	}
/*****************文章类别操作**********************************************/	
	public function type_show(){
		$arttype = D('art_type');
		$arttypelist = $arttype->select();
		$this->assign('arttypelist',$arttypelist);
		$this->display('./Public/system/art_type_show.html');
	}
	
	public function type_add(){
		$tid = $_GET['arttypeid'];
		if($tid){
			$array['news_tplname'] = '编辑';
			$typarr = D('art_type')->where('id='.$tid)->find();
		}else{
			$array['news_tplname'] = '添加';
		}
		$this->assign($array);
		$this->assign('tid',$tid);
		$this->assign('typarr',$typarr);
		$this->display('./Public/system/art_type_add.html');
	}
	
	public function type_del(){
		$tid = $_GET['arttypeid'];
		if(!$tid)$this->success('操作有误');		
		$at = D('art_type');
		$rs = $at->where("id='$tid'")->delete();
		$message = '删除失败';
		if($rs)$message = '删除成功';
		$this->success($message);
	}
	
	public function type_insert(){
		$at = D('art_type');
		if(empty($_POST['name']))$this->error('名称不允许为空');
		if($_POST['tid']){
			$date = array(
				'name' => $_POST['name'],
				'isnav' => $_POST['isnav'],
				'state' => $_POST['state']
			);
			$id = $at->where("id=$_POST[tid]")->save($date);
			if($id)$this->success('修改成功');
		}else{ //添加
			if($at->create()){
				$id = $at->add();
				if( false !== $id){		
				    $this->assign("jumpUrl",'?s=Admin-Art-Type_Add');
				}else{
					$this->error('文章添加失败！');
				}
			}
		}
		
		
	}
}
?>